/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author NGOCLINH-PC
 */
public class Subcriber {

    int ERECID;
    int MemberID;
    int CategoryID;

    public Subcriber() {
    }

    public Subcriber(int ERECID, int MemberID, int CategoryID) {
        this.ERECID = ERECID;
        this.MemberID = MemberID;
        this.CategoryID = CategoryID;
    }

    public int getERECID() {
        return ERECID;
    }

    public void setERECID(int ERECID) {
        this.ERECID = ERECID;
    }

    public int getMemberID() {
        return MemberID;
    }

    public void setMemberID(int MemberID) {
        this.MemberID = MemberID;
    }

    public int getCategoryID() {
        return CategoryID;
    }

    public void setCategoryID(int CategoryID) {
        this.CategoryID = CategoryID;
    }

    public ArrayList<Subcriber> Sselect() {
        try {
            ArrayList<Subcriber> listCate = new ArrayList<Subcriber>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM EmailRecieverEventCategory");
            while (rs.next()) {
                Subcriber cate = new Subcriber(rs.getInt(1), rs.getInt(2), rs.getInt(3));
                listCate.add(cate);// 
            }
            return listCate;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Subcriber> SselectbyCID(int CateID) {
        try {
            ArrayList<Subcriber> listCate = new ArrayList<Subcriber>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM EmailRecieverEventCategory where CategoryID=" + CateID);
            while (rs.next()) {
                Subcriber cate = new Subcriber(rs.getInt(1), rs.getInt(2), rs.getInt(3));
                listCate.add(cate);// 
            }
            return listCate;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public ArrayList<Subcriber> SselectbyMemID(int MemID) {
        try {
            ArrayList<Subcriber> listCate = new ArrayList<Subcriber>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM EmailRecieverEventCategory where MemberID=" + MemID);
            while (rs.next()) {
                Subcriber cate = new Subcriber(rs.getInt(1), rs.getInt(2), rs.getInt(3));
                listCate.add(cate);// 
            }
            return listCate;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void Sdelete(int CateID) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM EmailRecieverEventCategory WHERE ERECID=?");
            st.setInt(1, CateID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public static void SdeleteByMem(int mem) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM EmailRecieverEventCategory WHERE MemberID=?");
            st.setInt(1, mem);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Insert(int CateID, int memid) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("Insert into EmailRecieverEventCategory values(?,?)");
            st.setInt(1, memid);
            st.setInt(2, CateID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "INSERT Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
